<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>在线聊天室</title>
</head>
<body>
<input type="text" id="message">
<input onclick="sendMessage()" type="button" value="发送消息">
接收到的消息：
<p style="background:#AAA;" id="server_message"></p>

<script>
    var websocket = null;

    //判断当前浏览器是否支持websocket
    if (window.WebSocket) {
        websocket = new WebSocket("ws://localhost:9090/ws");

        websocket.onopen = function () {
            console.log("建立连接");
        }

        websocket.onclose = function () {
            console.log("断开连接");
        }

        websocket.onmessage = function (e) {
            console.log("接受到服务器消息:" + e.data);
            var server_message = document.getElementById("server_message");
            server_message.innerHTML += e.data + "<br/>";
            websocket.send('我是1');
        }


    } else {
        alert("当前浏览器不支持websocket");
    }

    function sendMessage() {
        var message = document.getElementById("message");
        websocket.send(message.value);
        message.value = "";
    }




</script>
</body>
</html>
